{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5.14 ms ± 865 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 并行化思想\n",
    "normal_list = range(10000)\n",
    "%timeit [i**2 for i in normal_list]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10.1 µs ± 1.49 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
     ]
    }
   ],
   "source": [
    "# arange用于创建等差数组\n",
    "# Numpy数组和普通列表的操作方式也是不同的， Numpy通过广播机制\n",
    "# 作用于每一个内部元素，是一种并行化的思想，普通list则作用于整体\n",
    "np_list = np.arange(10000)\n",
    "%timeit np_list**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np_list = np.zeros(100)\n",
    "np_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0.],\n",
       "       [0., 0.],\n",
       "       [0., 0.]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np_list = np.zeros((3,2))\n",
    "np_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1.],\n",
       "       [1., 1.],\n",
       "       [1., 1.]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np_list = np.ones((3,2))\n",
    "np_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[-0.00000000e+000,  3.11108980e+231,  7.41098469e-323],\n",
       "        [ 0.00000000e+000,  2.12199579e-314,  5.02034658e+175],\n",
       "        [ 2.65309590e-032,  2.44797900e-056,  1.39565547e-075]],\n",
       "\n",
       "       [[ 1.48949075e+161,  1.47763641e+248,  1.16096346e-028],\n",
       "        [ 7.69165785e+218,  1.35617292e+248,  9.11621610e-071],\n",
       "        [ 4.47259115e-062,  1.72585782e-047,  1.58687094e-047]]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 填充的指是随机数\n",
    "np_list = np.empty((2,3,3))\n",
    "np_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0.],\n",
       "       [0., 1., 0.],\n",
       "       [0., 0., 1.]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对角线全为1的矩阵 \n",
    "np_list = np.eye(3)\n",
    "np_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = [[1,2,3],[4,5,6]]\n",
    "np_list = np.array(data)\n",
    "np_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.        , 1.07142857, 1.14285714, 1.21428571, 1.28571429,\n",
       "       1.35714286, 1.42857143, 1.5       , 1.57142857, 1.64285714,\n",
       "       1.71428571, 1.78571429, 1.85714286, 1.92857143, 2.        ])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# linspace 在 a  b 间等间隔生成C个成员\n",
    "np_list = np.linspace(1,2,15)\n",
    "np_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function linspace in module numpy.core.function_base:\n",
      "\n",
      "linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)\n",
      "    Return evenly spaced numbers over a specified interval.\n",
      "    \n",
      "    Returns `num` evenly spaced samples, calculated over the\n",
      "    interval [`start`, `stop`].\n",
      "    \n",
      "    The endpoint of the interval can optionally be excluded.\n",
      "    \n",
      "    Parameters\n",
      "    ----------\n",
      "    start : scalar\n",
      "        The starting value of the sequence.\n",
      "    stop : scalar\n",
      "        The end value of the sequence, unless `endpoint` is set to False.\n",
      "        In that case, the sequence consists of all but the last of ``num + 1``\n",
      "        evenly spaced samples, so that `stop` is excluded.  Note that the step\n",
      "        size changes when `endpoint` is False.\n",
      "    num : int, optional\n",
      "        Number of samples to generate. Default is 50. Must be non-negative.\n",
      "    endpoint : bool, optional\n",
      "        If True, `stop` is the last sample. Otherwise, it is not included.\n",
      "        Default is True.\n",
      "    retstep : bool, optional\n",
      "        If True, return (`samples`, `step`), where `step` is the spacing\n",
      "        between samples.\n",
      "    dtype : dtype, optional\n",
      "        The type of the output array.  If `dtype` is not given, infer the data\n",
      "        type from the other input arguments.\n",
      "    \n",
      "        .. versionadded:: 1.9.0\n",
      "    \n",
      "    Returns\n",
      "    -------\n",
      "    samples : ndarray\n",
      "        There are `num` equally spaced samples in the closed interval\n",
      "        ``[start, stop]`` or the half-open interval ``[start, stop)``\n",
      "        (depending on whether `endpoint` is True or False).\n",
      "    step : float, optional\n",
      "        Only returned if `retstep` is True\n",
      "    \n",
      "        Size of spacing between samples.\n",
      "    \n",
      "    \n",
      "    See Also\n",
      "    --------\n",
      "    arange : Similar to `linspace`, but uses a step size (instead of the\n",
      "             number of samples).\n",
      "    logspace : Samples uniformly distributed in log space.\n",
      "    \n",
      "    Examples\n",
      "    --------\n",
      "    >>> np.linspace(2.0, 3.0, num=5)\n",
      "    array([ 2.  ,  2.25,  2.5 ,  2.75,  3.  ])\n",
      "    >>> np.linspace(2.0, 3.0, num=5, endpoint=False)\n",
      "    array([ 2. ,  2.2,  2.4,  2.6,  2.8])\n",
      "    >>> np.linspace(2.0, 3.0, num=5, retstep=True)\n",
      "    (array([ 2.  ,  2.25,  2.5 ,  2.75,  3.  ]), 0.25)\n",
      "    \n",
      "    Graphical illustration:\n",
      "    \n",
      "    >>> import matplotlib.pyplot as plt\n",
      "    >>> N = 8\n",
      "    >>> y = np.zeros(N)\n",
      "    >>> x1 = np.linspace(0, 10, N, endpoint=True)\n",
      "    >>> x2 = np.linspace(0, 10, N, endpoint=False)\n",
      "    >>> plt.plot(x1, y, 'o')\n",
      "    [<matplotlib.lines.Line2D object at 0x...>]\n",
      "    >>> plt.plot(x2, y + 0.5, 'o')\n",
      "    [<matplotlib.lines.Line2D object at 0x...>]\n",
      "    >>> plt.ylim([-0.5, 1])\n",
      "    (-0.5, 1)\n",
      "    >>> plt.show()\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 查看API文档的方式\n",
    "help(np.linspace)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用np.random.standard_normal() 随机生成200只股票 504个交易日符合正态分布的涨跌幅数据\n",
    "# 504=252*2 两年美股交易日总数 \n",
    "# 正态分布 即为高斯分布\n",
    "\n",
    "stock_cnt = 200\n",
    "view_days = 504\n",
    "# 生成符合正态分布均值期望为0， 标准差为1\n",
    "stock_day_change = np.random.standard_normal((stock_cnt, view_days))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(200, 504)\n"
     ]
    }
   ],
   "source": [
    "print(stock_day_change.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.63393563 -0.91908786 -1.06350784  0.65654618 -0.01555733]]\n"
     ]
    }
   ],
   "source": [
    "# 第一支股票 前五日变化\n",
    "print(stock_day_change[0:1, :5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-1.67268227 -0.96930545  1.26483169 -0.34381053  0.13991881]\n",
      " [-0.48382757  0.59859394 -0.20968748  0.42412547  0.9558252 ]]\n"
     ]
    }
   ],
   "source": [
    "# 最后两只股票 最后五日的变化\n",
    "print(stock_day_change[-2:, -5:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  0 -1  0  0]]\n"
     ]
    }
   ],
   "source": [
    "# Numpy 内部实现的机制全部是引用操作，所以取数据时采用copy的方式\n",
    "one_data = stock_day_change[0:1, :5].copy()\n",
    "print(one_data.astype(int))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.63 -0.92 -1.06  0.66 -0.02]]\n"
     ]
    }
   ],
   "source": [
    "# np.around 规整float数据 保留两位小数\n",
    "one_data = stock_day_change[0:1, :5].copy()\n",
    "print(np.around(one_data, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "# np.nan_to_num() 函数用0来填充nan"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[False, False, False,  True, False],\n",
       "       [False,  True, False, False, False]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 逻辑条件筛选\n",
    "# 找出切片内涨幅超过0.5的股票\n",
    "mask = stock_day_change[0:2, :5] > 0.5\n",
    "mask"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.65654618, 1.3375079 ])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对mask使用\n",
    "tmp_data =  stock_day_change[0:2, :5] \n",
    "tmp_data[mask]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.63393563, -0.91908786, -1.06350784,  1.        , -0.01555733],\n",
       "       [-0.95848805,  1.        ,  0.43285531, -0.75937955,  0.0297567 ]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 找出temp切片中 > 0.5 的元素 并赋值为1\n",
    "tmp_data[tmp_data>0.5] = 1\n",
    "tmp_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通用序列函数\n",
    "# np.all() 判断序列中所有元素是否满足条件 返回true false\n",
    "np.all(tmp_data > 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# np.any 判断序列中是否有元素满足条件\n",
    "np.any(tmp_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.63393563, -0.91908786,  1.26483169,  1.        ,  0.13991881],\n",
       "       [-0.48382757,  1.        ,  0.43285531,  0.42412547,  0.9558252 ]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# maximum()  和 minimum() 函数 对两组元素进行比较， maximum()结果集取大， minimum() 结果集取小\n",
    "np.maximum(stock_day_change[0:2, :5], stock_day_change[-2:, -5:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  0, -1,  1,  0],\n",
       "       [ 0,  1,  0,  0,  0]])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# np.unique() 函数 将不同的元素组成新的序列\n",
    "change_int  = stock_day_change[0:2, :5].astype(int)\n",
    "change_int"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1,  0,  1])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.unique(change_int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.28515223, -0.14441998,  2.06350784, -1.01555733],\n",
       "       [ 1.95848805, -0.56714469, -1.19223486,  0.78913625]])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# np.diff() 函数 前后相邻的两个数值进行减法运算 默认情况下 axis = 1  横行移动 在行之间进行运算\n",
    "np.diff(stock_day_change[0:2, :5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.67268227, -0.96930545,  1.        , -0.34381053,  1.        ],\n",
       "       [-0.48382757,  1.        , -0.20968748,  1.        ,  1.        ]])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# np.where()  在数据筛选， 改造中有非常大的作用\n",
    "# 类似于三幕运算符，条件 ， 符合：不符合\n",
    "tmp_test = stock_day_change[-2:, -5:].copy()\n",
    "np.where(tmp_test > 0, 1, tmp_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1, 0, 0],\n",
       "       [0, 1, 0, 0, 1]])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 如果逻辑表达式为复合逻辑 则使用np.logical_and() 和 np.logical_or()\n",
    "np.where(np.logical_or(tmp_test > 0.5, tmp_test < -0.5), 1, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(200, 504)"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据本地序列化操作, 不需要后缀 save会自动存储为.npy文件\n",
    "np.save('./stock_day_change', stock_day_change)\n",
    "stock_day_change_02 = np.load('./stock_day_change.npy')\n",
    "stock_day_change_02.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
